package monitor.JavaCrash;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.example.leechyli.crashmonitor.MainActivity;
import com.example.leechyli.crashmonitor.MyApplication;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;

import monitor.MonitorUtil;

/**
 * Created by leechyli on 2017/7/14.
 */

public class JavaCrashHandler implements UncaughtExceptionHandler{

    private static String TAG = MonitorUtil.TAG;
    private static JavaCrashHandler ourInstance = new JavaCrashHandler();
    private static MyApplication app;
    private static PendingIntent restartIntent;
    ArrayList<Activity> activities = new ArrayList<Activity>();

    public static JavaCrashHandler getInstance()
    {
        return ourInstance;
    }

    public static void registerJavaCrashHandler(MyApplication app){
        app = app;
        Thread.setDefaultUncaughtExceptionHandler(JavaCrashHandler.getInstance());

        Intent intent = new Intent(app.getApplicationContext(), MainActivity.class);
        restartIntent = PendingIntent.getActivity(
                app.getApplicationContext(), 0, intent,
                Intent.FLAG_ACTIVITY_NEW_TASK);

        Log.w(TAG,"JavaCrashHandler has been registered.");
    }

    private JavaCrashHandler()
    {
    }

    @Override
    public void uncaughtException(Thread thread, final Throwable throwable) {
        /**
         * 捕获异常
         */

        Log.w(TAG, "in uncaughtException");
        String stackTraceInfo = getStackTraceInfo(throwable);
        MonitorUtil.reportError(stackTraceInfo, true);
        myUncaughtExceptionToDo();
    }

    /**
     * 自定义的对异常的处理
     */
    public void myUncaughtExceptionToDo() {

        Log.e(TAG,">> exit start");
//        try{
//            Thread.sleep(1000);
//        }catch (InterruptedException e){
//            Log.e(TAG, "error : ", e);
//        }
//
//

        Log.e(TAG,">> exit start @2");

        //退出程序
//        AlarmManager mgr = (AlarmManager)this.app.getSystemService(Context.ALARM_SERVICE);
//        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 5000,
//                restartIntent); // 1秒钟后重启应用

        Log.e(TAG,">> restart after 8s.");
//        app.finishActivity();

//        // 关闭应用
        System.exit(0);

        Log.e(TAG,">> exit restart after 8s.");

    }


    /**
     * 获取Exception崩溃堆栈
     */
    public static String getStackTraceInfo(final Throwable ex) {
        String trace = "Error type: Java Crash\n";
        try {
            /**
             *获取调用此方法的application context
             */

            Writer writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            ex.printStackTrace(printWriter);
            trace = writer.toString();
            printWriter.close();
        } catch (Exception e) {
            Log.e(TAG,"getStackTraceInfo failed.");
            ex.printStackTrace();
            return trace;
        }
        return trace;
    }
}

